SpringBoot官方文档翻译(十九):运行您的应用

19. Running Your Application(运行您的应用)

1
2
3
4
5
One of the biggest advantages of packaging your application as     
a jar and using an embedded HTTP server is that you can run your
application as you would any other. Debugging Spring Boot
applications is also easy. You do not need any special IDE plugins
or extensions.

将您的应用打包成一个jar并且使用内置的HTTP服务器有一个巨大的优势就是您可以直接运行您的应用。调试Spring Boot应用也是非常简单的。您不需要任何特殊的IDE插件或者扩展。

1
2
3
This section only covers jar based packaging. If you choose     
to package your application as a war file, you should refer
to your server and IDE documentation.

本节只讲述基于jar包打包的方式。如果你选择将您的应用打包成一个war包,您应该参考
到您的服务器和IDE文档。

19.1 Running from an IDE(从一个IDE钟运行)

1
2
3
4
5
You can run a Spring Boot application from your IDE as a simple     
Java application. However, you first need to import your project.
Import steps vary depending on your IDE and build system. Most
IDEs can import Maven projects directly. For example, Eclipse
users can select Import…  → Existing Maven Projects from the File menu.

您可以从您的IDE将Spring Boot应用作为一个简单的java应用来运行。然而,您首先需要导入您的工程。导入步骤根据您的IDE构建系统的不同而变化。大部分IDE能够直接导入Maven工程。举个例子,Eclipse用户可以选择File-Import-Existing Maven Projects菜单。

1
2
3
4
If you cannot directly import your project into your IDE,     
you may be able to generate IDE metadata by using a build plugin.
Maven includes plugins for Eclipse and IDEA. Gradle offers plugins
for various IDEs.

如果您不能直接从IDE中导入您的工程,您可以通过构建插件生成IDE metadata。Maven包含Eclipse和IDEA的插件。Gradle为大部分IDE提供了插件

1
2
3
4
If you accidentally run a web application twice, you see     
a “Port already in use” error. STS users can use the Relaunch 
button rather than the Run button to ensure that any existing
instance is closed.

如果您偶然的运行了2次web应用,您会发现“Port already in use”端口已经被使用的报错.STS使用者,能够使用Relaunch按钮来避免这个问题,如果使用Run按钮请确认任何实例都已经处于关闭状态。

19.2 Running as a Packaged Application(作为一个大包应用运行)

1
2
3
If you use the Spring Boot Maven or Gradle plugins to create     
an executable jar, you can run your application using java -jar,
as shown in the following example:

如果您使用Spring Boot Maven或者Gradle插件创建了一个可执行的jar,您可以通过java -jar命令来运行它,如下例子:

1
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar

1
2
3
It is also possible to run a packaged application with remote     
debugging support enabled. Doing so lets you attach a debugger
to your packaged application, as shown in the following example:

同样可以通过远程调试模式运行一个已经打包的应用,如果远程调试开启的情况下。这样做的话您可以链接一个断点到您的已经打包的应用中,如下例子:

1
2
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar

19.3 Using the Maven Plugin(使用Maven插件)

1
2
3
4
The Spring Boot Maven plugin includes a run goal that can     
be used to quickly compile and run your application. Applications
run in an exploded form, as they do in your IDE. The following
example shows a typical Maven command to run a Spring Boot application:

Spring Boot的Maven插件包含了一个运行目标可以快速编译和运行您的应用。应用像在您的IDE中以分解的形式运行。接下去的例子展示了一个典型的Maven命令运行Spring Boot 应用。

1
$ mvn spring-boot:run

1
2
You might also want to use the MAVEN_OPTS operating system     
environment variable, as shown in the following example:

您也许也想使用MAVEN_OPTS操作系统环境变量,如下例子:

1
$ export MAVEN_OPTS=-Xmx1024m

19.4 Using the Gradle Plugin(使用Gradle插件)

1
2
3
4
The Spring Boot Gradle plugin also includes a bootRun task that can     
be used to run your application in an exploded form. The bootRun 
task is added whenever you apply the org.springframework.boot and 
java plugins and is shown in the following example:

Spring Boot的Gradle插件同样映入了bootRun任务,能够运用分解的方式运行您的应用。bootRun任务将会被自动引入,如果您引入了org.springframework.boot和java插件。如下例子:

1
$ gradle bootRun

1
2
You might also want to use the JAVA_OPTS operating system     
environment variable, as shown in the following example:

您也许想使用JAVA_OPTS操作系统环境变量,如下例子:

1
$ export JAVA_OPTS=-Xmx1024m

19.5 Hot Swapping(热交换)

1
2
3
4
Since Spring Boot applications are just plain Java applications,     
JVM hot-swapping should work out of the box. JVM hot swapping is
somewhat limited with the bytecode that it can replace. For a
more complete solution, JRebel can be used.

由于Spring Boot应用程序只是普通的Java应用程序,所以JVM热插拔应该可以开箱即用。JVM热插拔是
在可以替代的字节码方面有所限制。需要更完整的解决方案,可以使用JRebel。

1
2
3
4
The spring-boot-devtools module also includes support for     
quick application restarts. See the Chapter 20, Developer
Tools section later in this chapter and theHot swapping
“How-to” for details.

spring-boot-devtools模块也宝行了支持快速重启应用的功能。查看章节20,开发者工具章节接下去会讲这点,已经热插拔相关细节。

分享到